﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Quota_Create
    Inherits System.Web.UI.UserControl

    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            LoadStoge()
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
        End If
    End Sub
    Protected Sub LoadStoge()
        Using Data As New VSEntities
            Dim p = (From q In Data.Stores Order By q.Title Select q.StoreId, q.Title).ToList
            ddlStoge.AppendDataBoundItems = True
            ddlStoge.Items.Add(New ListItem("---Chọn---", 0))
            ddlStoge.DataTextField = "Title"
            ddlStoge.DataValueField = "StoreId"
            ddlStoge.DataSource = p
            ddlStoge.DataBind()
        End Using
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            ' Dim k = (From q In data.Quotas Where q.QuotaNumber = txtQuotaNumber.Text Select q).SingleOrDefault
            'If Not k Is Nothing Then
            '    Excute_Javascript("Alertbox('Quota number đã tồn tại.');", Me.Page, True)
            '    Exit Sub

            'End If
            Dim p As New VSModel.Quota
            Try
                p.QuotaNumber = Now.Date.ToString("yyyy-MM-dd") & "-" & txtProductCode.Text
                p.StoreId = ddlStoge.SelectedValue
                p.Total = txtTotal.Text
                p.ProductCode = txtProductCode.Text.Trim
                p.Created = Now
                p.Creator = Session("Username")
                data.Quotas.AddObject(p)
                Data.SaveChanges()
                'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("AlertboxRedirect('Cập nhật dữ liệu thành công.','List.aspx');", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
    Protected Sub hplSearch_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles hplSearch.Init
        hplSearch.NavigateUrl = "~/Page/BanHang/Popup.aspx?keepThis=true&TB_iframe=true&height=300&width=500&modal=true"
    End Sub
#End Region

End Class
